//
//  SwitchButton.m
//  DingDongXiChe
//
//  Created by wtjr on 2018/1/4.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "SwitchButton.h"

@interface SwitchButton() {
    CGFloat _buttonW;
    CGFloat _buttonH;
}
@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) UIView *switchBackView;

@property (nonatomic, strong) BaseButton *leftButton;

@property (nonatomic, strong) BaseButton *rightButton;

@end

@implementation SwitchButton

#pragma mark - Init

- (void)initData {
    _buttonW = (kUI_WIDTH - 20) / 6;
    _buttonH = 24;
}

- (void)initSubViews {
    [self addSubview:self.mainView];
}

- (void)autoLayoutSubViews {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
}

#pragma mark - BaseMethod


#pragma mark - Interface


#pragma mark - Delegate


#pragma mark - TouchEvent

- (void)clickLeftButton {
    //切换动画
    [self.switchBackView mas_updateConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(@(_buttonW * 2));
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self.mainView layoutIfNeeded];
    }];
    //点击完成回调，返回角标0
    if (self.clickCompleted) {
        self.clickCompleted(0);
    }
}

- (void)clickRightButton {
    //切换动画
    [self.switchBackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(_buttonW * 3));
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self.mainView layoutIfNeeded];
    }];
     //点击完成回调，返回角标1
    if (self.clickCompleted) {
        self.clickCompleted(1);
    }
}

#pragma mark - Setter

- (void)setLeftTitle:(NSString *)leftTitle {
    self.leftButton.title = leftTitle;
}

- (void)setRightTitle:(NSString *)rightTitle {
    self.rightButton.title = rightTitle;
}

#pragma mark - Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        
        //添加动画显示view
        UIView *switchBackView = [UIView new];
        switchBackView.layer.cornerRadius = _buttonH / 2;
        switchBackView.layer.masksToBounds = YES;
        switchBackView.layer.borderColor = KCOLOR_font_tint.CGColor;
        switchBackView.layer.borderWidth = 0.5;
        self.switchBackView = switchBackView;
        [_mainView addSubview:switchBackView];
        [switchBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(_buttonW * 2));
            make.centerY.equalTo(@0);
            make.width.equalTo(@(_buttonW));
            make.height.equalTo(@(_buttonH));
        }];
        
        //添加左侧按钮
        BaseButton *leftButton = [BaseButton new];
        [leftButton setButtonStyle:@"左边" titleColor:KCOLOR_font_light titleFont:kFONT_mainFont(12) texrAlignment:NSTextAlignmentCenter];
        [leftButton addTarget:self action:@selector(clickLeftButton) forControlEvents:UIControlEventTouchUpInside];
        self.leftButton = leftButton;
        [_mainView addSubview:leftButton];
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(_buttonW * 2));
            make.centerY.equalTo(@0);
            make.width.height.equalTo(switchBackView);
        }];
        
        //添加右侧按钮
        BaseButton *rightButton = [BaseButton new];
        [rightButton setButtonStyle:@"右边" titleColor:KCOLOR_font_light titleFont:leftButton.titleLabel.font texrAlignment:NSTextAlignmentCenter];
        [rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
        self.rightButton = rightButton;
        [_mainView addSubview:rightButton];
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(leftButton.mas_right);
            make.centerY.equalTo(@0);
            make.width.height.equalTo(switchBackView);
        }];
    }
    return _mainView;
}

@end
